package Panel;
/**
 * Created by win10 on 2017/6/13.
 */
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.AffineTransform;
import javax.swing.JPanel;

class DrawPanel extends JPanel {
    /**
     *
     */
	private Graphics2D g2;
	private double startX,startY,endX,endY,w,h;
	private double mouseX,mouseY;
    private static final long serialVersionUID = 1L;
  static  ShapeArrayList shapes=new ShapeArrayList();

    public DrawPanel(){
        this.setPreferredSize(new Dimension(400,400));
        this.setLayout(new FlowLayout(FlowLayout.RIGHT));
        
    }
    
    public void showGui(){
    	this.addMouseMotionListener(new MouseMotionAdapter(){
    		public void mouseDragged(MouseEvent e){
    			mouseX=e.getX();
    			mouseY=e.getY();
    			selectDraw();   // 调用画图，根据用户选择的状态画图
    		}
    	});

    	this.addMouseListener(new MouseAdapter(){

            public void mousePressed(MouseEvent e){
                startX = e.getX();
                startY = e.getY();
               
            }

            public void mouseReleased(MouseEvent e){
            	    g2 = (Graphics2D) getGraphics();
            	    
                	endX = e.getX();
                    endY = e.getY();
                    w =endX-startX;
                    h =endY-startY;
                    mouseReleaseDraw();            
            }
        });
        }
    public void drawLine(){    //可以在画板上随便画
    	endX=mouseX;
    	endY=mouseY;
    	Drawline dl=new Drawline(g2,startX,startY,endX,endY);
    	shapes.add(dl);
    	startX=endX;
    	startY=endY;
    }
    public void selectDraw(){
    	double radius;
		
		 g2 = (Graphics2D) getGraphics();
		 SetColor setColor=new SetColor(g2);
         w =mouseX-startX;
         h =mouseY-startY;
         radius=Math.sqrt(w*w+h*h);
    	int statu=0;
    	  for(int i=0;i<ClickAction.drawStatus.length;i++){
         	 if(ClickAction.drawStatus[i]!=0){
         		 statu=i;
         		 System.out.println("c   "  +i);
         	//	 break;
         	 }
          }
    	  switch(statu){
    	  case 1:     //画圆形
    		  g2.clearRect(0, 0, 1000, 1000);
    		  Circle ctest=new Circle(g2,startX,startY,radius);
    	//	  ctest.translate(10, 40);
    		  break;
    	  case 2:      //画三角形
    		  g2.clearRect(0, 0, 1000, 1000);
    		  Triangle ttest=new Triangle(g2,startX,startY,radius);
    		  break;
    	  case 3: // 话四边形
    		  g2.clearRect(0, 0, 1000, 1000);
    		  Rectangle test = new Rectangle(g2,startX,startY,w,h);
    		  break;
    	  case 6:  //移动所有图形
    		  g2.clearRect(0, 0, 1000, 1000);
    		  translate(w,h);
    		  break;
    	  case 7:   //旋转所有图形
    		  g2.clearRect(0, 0, 1000, 1000);
    		  rotate(radius/10,startX,startY);
    		  break;
    	  case 8:
    		  g2.clearRect(0, 0, 1000, 1000);
    		  scale(radius/20,radius/20);
    		  break;
    	  default:
    		  drawLine();
    			break;
    			 
    	  }
    }
    public void mouseReleaseDraw(){   //根据鼠标点击的画图功能画图；
    	double radius;
		
		 g2 = (Graphics2D) getGraphics();
	//	 g2.setColor(Color.BLUE);
		 SetColor setColor=new SetColor(g2);
         w =mouseX-startX;
         h =mouseY-startY;
         if(w<0) w=0-w;
         if(h<0) h=0-h;
         radius=Math.sqrt(w*w+h*h);
    	int statu=0;
    	  for(int i=0;i<ClickAction.drawStatus.length;i++){
         	 if(ClickAction.drawStatus[i]!=0){
         		 statu=i;
         		 System.out.println("c   "  +i);
         	//	 break;
         	 }
          }
    	  switch(statu){
    	  case 1:             //点击是圆形，则画圆形；
    		  g2.clearRect(0, 0, 1000, 1000);
    		  Circle ctest=new Circle(g2,startX,startY,radius);
    		  shapes.add(ctest);
    		  break;
    	  case 2:           //点击的是三角形画三角形
    		  g2.clearRect(0, 0, 1000, 1000);
    		  Triangle ttest=new Triangle(g2,startX,startY,radius);
    		  shapes.add(ttest);
    		  break;
    	  case 3:   		//点击的是四边形，画四边形
    		  g2.clearRect(0, 0, 1000, 1000);
    		  Rectangle test = new Rectangle(g2,startX,startY,w,h);
    		  shapes.add(test);
    		  break;
    	  case 4:
    	//	  shapes.showShape();
    		  break;
    	  default:
    	//	  drawLine();
    			break;
    			 
    	  }
    }
 
    public void translate(double x,double y){   //移动图形函数
    	shapes.translate(x, y);
    }
    public void rotate(double numble,double x,double y){  //旋转图形
    	shapes.rolate(numble,x,y);
    }
    
    public void scale(double x,double y){    //缩放图形
    	
    	
    	shapes.scale(x, y);
    //	shapes.translate(2, 550);
    }
    
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;//将Graphics对象转换为Graphics2D对象
        //绘制矩形
//        double leftX = 100;//定义开始绘制的坐标
//        double topY = 100;
//        double width = 200;//定义绘制宽度
//        double height = 150;//定义绘制高度
//        Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);//创建矩形对象
//        g2.draw(rect);//将矩形对象绘制于Panel面板上
//        AffineTransform transform = new AffineTransform();
//        transform.rotate(45 * Math.PI / 180, 90, 200); // 围绕(90,200)点旋转图形45度
//        g2.setTransform(transform);
//        g2.draw(rect);
        

        //绘制线条
    }
   
    
    

    
    }
    

    